<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="应用id" prop="appUniqueId">
      <el-input v-model="dataForm.appUniqueId" placeholder="应用id"></el-input>
    </el-form-item>
    <el-form-item label="日志类型" prop="logType">
      <el-input v-model="dataForm.logType" placeholder="日志类型"></el-input>
    </el-form-item>
    <el-form-item label="单号" prop="transactionId">
      <el-input v-model="dataForm.transactionId" placeholder="单号"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="logStatus">
      <el-input v-model="dataForm.logStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="状态描述" prop="logStatusMsg">
      <el-input v-model="dataForm.logStatusMsg" placeholder="状态描述"></el-input>
    </el-form-item>
    <el-form-item label="请求参数" prop="actionParameters">
      <el-input v-model="dataForm.actionParameters" placeholder="请求参数"></el-input>
    </el-form-item>
    <el-form-item label="步骤列表" prop="steps">
      <el-input v-model="dataForm.steps" placeholder="步骤列表"></el-input>
    </el-form-item>
    <el-form-item label="日志生成的时间" prop="logDate">
      <el-input v-model="dataForm.logDate" placeholder="日志生成的时间"></el-input>
    </el-form-item>
    <el-form-item label="执行时长(毫秒)" prop="exeTime">
      <el-input v-model="dataForm.exeTime" placeholder="执行时长(毫秒)"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          appUniqueId: '',
          logType: '',
          transactionId: '',
          logStatus: '',
          logStatusMsg: '',
          actionParameters: '',
          steps: '',
          logDate: '',
          exeTime: '',
          createDate: ''
        },
        dataRule: {
          appUniqueId: [
            { required: true, message: '应用id不能为空', trigger: 'blur' }
          ],
          logType: [
            { required: true, message: '日志类型不能为空', trigger: 'blur' }
          ],
          transactionId: [
            { required: true, message: '单号不能为空', trigger: 'blur' }
          ],
          logStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          logStatusMsg: [
            { required: true, message: '状态描述不能为空', trigger: 'blur' }
          ],
          actionParameters: [
            { required: true, message: '请求参数不能为空', trigger: 'blur' }
          ],
          steps: [
            { required: true, message: '步骤列表不能为空', trigger: 'blur' }
          ],
          logDate: [
            { required: true, message: '日志生成的时间不能为空', trigger: 'blur' }
          ],
          exeTime: [
            { required: true, message: '执行时长(毫秒)不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapplog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.appUniqueId = data.subAppLog.appUniqueId
                this.dataForm.logType = data.subAppLog.logType
                this.dataForm.transactionId = data.subAppLog.transactionId
                this.dataForm.logStatus = data.subAppLog.logStatus
                this.dataForm.logStatusMsg = data.subAppLog.logStatusMsg
                this.dataForm.actionParameters = data.subAppLog.actionParameters
                this.dataForm.steps = data.subAppLog.steps
                this.dataForm.logDate = data.subAppLog.logDate
                this.dataForm.exeTime = data.subAppLog.exeTime
                this.dataForm.createDate = data.subAppLog.createDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapplog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'appUniqueId': this.dataForm.appUniqueId,
                'logType': this.dataForm.logType,
                'transactionId': this.dataForm.transactionId,
                'logStatus': this.dataForm.logStatus,
                'logStatusMsg': this.dataForm.logStatusMsg,
                'actionParameters': this.dataForm.actionParameters,
                'steps': this.dataForm.steps,
                'logDate': this.dataForm.logDate,
                'exeTime': this.dataForm.exeTime,
                'createDate': this.dataForm.createDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
